<?php
/*
 * Author: Quan Van Sinh
 * Email: sinhvnb@vietnambiz.com
 * Update: NghiaTruong
 * Email: nghiatruong@vietnambiz.com
 * Date: 16-3-2011
 */
checkLoginHeader();
checkPermission();
$permission = new permission();
$permission->permissionAction();

?>
<?php include_once $config ['admin_theme_path'].$config ['admin_theme'].'/header.php'; ?>


<script type="text/javascript">
    
    function validateAddPermissionForm(thisform){
        {
            with (thisform)
              {
              if (validate_required(username,"User name must be filled out!")==false)
                {edPermissionname.focus();return false;}
              if (validate_required(denyModules,"Deny modules must be filled out!")==false)
                {edEmail.focus();return false;}
              }
            }
        }
    function validateEditPermissionForm(thisform){
        {
            with (thisform)
              {
              if (validate_required(edUsername,"User name must be filled out!")==false)
                {edPermissionname.focus();return false;}
              if (validate_required(edDenyModules,"Deny modules must be filled out!")==false)
                {edEmail.focus();return false;}
              }
            }
        }

        
    function addPermission(){
        document.getElementById('permissionAction').style.display = '';
        document.getElementById('formAddPermission').style.display = '';
        document.getElementById('formEditPermission').style.display = 'none';
    }
    function editPermission(id){
        document.getElementById('permissionAction').style.display = '';
        document.getElementById('formAddPermission').style.display = 'none';
        document.getElementById('formEditPermission').style.display = '';
        $.ajax({
            url: '/<?php echo $config ['admin_section']; ?>/permission',
            type: 'POST',
            dataType: 'json',
            data: "id="+id+"&permissionActionType=editPermission",
            timeout: 1000,
            error: function(){
                alert('Error loading permission infomation');
            },
            success: function(data){
                $("#edId").val(data.id);
                $("#edUsername").val(data.username);
                $("#edDenyModules").val(data.deny_modules);
                $("#permissionActionType").val('updatePermission'); 
            }
        });
    }

    

    function deletePermission(id,txtAlert){
        if (confirm("Do you want to delete permission "+txtAlert+"?")){
            $.ajax({
                url: '/<?php echo $config ['admin_section']; ?>/permission',
                type: 'POST',
                data: "id="+id+"&permissionActionType=deletePermission",
                error: function(){
                    alert('Error loading permission infomation');
                },
                success: function(){
                    window.location = "/<?php echo $config ['admin_section']; ?>/permission";
                }
            });
        }
    }

    $(document).ready(function(){
        document.getElementById('permissionAction').style.display = 'none';
        setTimeout("closeResultMessages()",5000);
    });
    function closeResultMessages(){
        document.getElementById('rsMessages').style.display = 'none';
    }
    function closeHtml(id){
        document.getElementById(id).style.display = 'none';
    }
    
</script>

<div id="wrapper">
            <div id="content" style="width: 98%;">

                <div id="permissionAction">
                <div id="box">
                      <div id="rightnow">
                          <h3 class="reallynow">
                            <span>Permission Actions</span>
                            <a href="javascript: closeHtml('permissionAction');">Close</a><br />
                          </h3>
                      </div>
                      <div id="formAddPermission">
                          <form method="post" action="/<?php echo $config ['admin_section']; ?>/permission" id="form" name="formAddPermission" onsubmit="return validateAddPermissionForm(this);" style="padding-top: 10px">
                          <fieldset id="fsAddPermission">
                            <legend>Add permission</legend>
                            <label for="username">User name : </label>
                            <input style="width: 200px" type="text" tabindex="1" id="username" name="username"/>
                            <input type="hidden" id="permissionActionType" name="permissionActionType" value="addPermission"/>
                            <br/>
                            <label for="denyModules">Deny modules : </label>
                            <input style="width: 200px" type="text" tabindex="2" id="denyModules" name="denyModules"/>
                          </fieldset>
                      <div align="center">
                      <input type="submit" value="Create permission" name="addPermissionSubmit" id="addPermissionSubmit"/>
                      <input type="reset" id="AddPermissionReset"/>
                      </div>
                    </form>
                    </div>

                    <div id="formEditPermission">
                        <form method="post" action="/<?php echo $config ['admin_section']; ?>/permission" id="form" onsubmit="javascript: return validateEditPermissionForm(this);" name="formEditPermission">
                          <fieldset id="fsEditPermission">
                            <legend>Edit permission</legend>
                            <label for="username">User name : </label>
                            <input style="width: 200px" type="text" tabindex="1" id="edUsername" name="edUsername"/>
                            <input type="hidden" id="edId" name="edId"/>
                            <input type="hidden" id="permissionActionType" name="permissionActionType" value="updatePermission"/>
                            <br/>
                            <label for="denymodules">Deny module : </label>
                            <input style="width: 200px" type="text" tabindex="2" id="edDenyModules" name="edDenyModules"/>
                          </fieldset>

                      <div align="center">
                          <input type="submit" value="Update permission infomation" name="editPermissionSubmit" id="editPermissionSubmit"/>
                      </div>
                    </form>
                    </div>
                </div>
                <div style="height:10px"></div>
                </div>

                
                <div id="rightnow">
                    <h3 class="reallynow">
                        <span>Permissions</span>
                        <span style="float: right"><a class="add" href="javascript: addPermission();">Add permission</a></span>
                        <br />
                    </h3>
                    <div class="youhave">
                        <div id="rsMessages" style="text-align: center; color: red; font-weight: bolder"><? if(isset($_SESSION['rsMessages'])){echo $_SESSION['rsMessages']; $_SESSION['rsMessages']='';} ?></div>
                        <table width="100%">

                            <tbody>
                                <tr>
                                    <td width="40px">&nbsp;</td>
                                    <td><b>User name</b></td>
                                    <td><b>Deny module</b></td>
                                    <td width="60px"><b>Action</b></td>
                                </tr>
                                <?php
                                    $rsAllPermission = $permission->getAllPermission();
                                    $i=0;
                                    while($rs=$db->fetch_array($rsAllPermission)){
                                        $i++;
                                ?>
                                <tr>
                                    <td class="a-center"><? echo $i;?></td>
                                    <td><? echo $rs['username'];?></td>
                                    <td><span style="color: red;"><? echo $rs['deny_modules'];?></span></td>
                                    <td><a href="javascript: editPermission('<?php echo $rs['id'];?>');"><img height="16" width="16" title="Edit permission" src="<? echo $config ['admin_images_url'];?>/icons/user_edit.png"/></a><a href="javascript: deletePermission('<?php echo $rs['id'];?>','<?php echo $rs['username'];?>');"><img height="16" width="16" title="Delete permission" src="<?php echo $config ['admin_images_url'];?>/icons/user_delete.png"/></a></td>
                                </tr>
                                <?php }?>
                                </tbody>
                        </table>
                    </div>
                  </div>                  
            </div>


      </div>
<?php include_once $config ['admin_theme_path'].$config ['admin_theme'].'/footer.php'; ?>